package com.smartthinking.destinomusical.parser;

import java.io.IOException;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;

import com.smartthinking.destinomusical.extra.Errores;
import com.smartthinking.destinomusical.extra.Tool;
import com.smartthinking.destinomusical.obj.Categoria;

public class ParserCategoria extends BaseFeedParser {
	final ArrayList<Categoria> lista = new ArrayList<Categoria>();
	static final String ROOT = "RESULTS";
	int total;
	Categoria obj;

	public ParserCategoria(String feedUrl) {
		super(feedUrl);
	}

	public ArrayList<Categoria> parse() {

		RootElement root = new RootElement(ROOT);

		root.getChild("TOTAL").setStartElementListener(
				new StartElementListener() {
					@Override
					public void start(Attributes attributes) {
						total = Integer.valueOf(attributes.getValue("rows"));
						System.out.println(total);
					}
				});

		Element row = root.getChild("ROW");
		row.setStartElementListener(new StartElementListener() {

			@Override
			public void start(Attributes arg0) {
				obj = new Categoria();

			}
		});
		row.getChild("cat_cve").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						obj.setClave(body);
					}
				});
		row.getChild("cat_des").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("imagen url:" + body);
						obj.setDescripcion(body);
					}
				});
		row.getChild("cat_imagen").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						System.out.println("imagen url:" + body);
						obj.setImgURL(body);
					}
				});
		row.getChild("cat_cve_padre").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
							obj.setClave_padre(body);
					}
				});
		row.getChild("hijos").setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						if(body==null){
							obj.setHijos("");
						System.out.println("Nuuuuuuuuuuuuuuuuuuuuuull");}
						else
							obj.setHijos(body);
					}
				});

		row.setEndElementListener(new EndElementListener() {

			@Override
			public void end() {
				lista.add(obj);

			}
		});
		try {
			Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
					root.getContentHandler());
		} catch (IOException e) {
			Errores.NO_CONNECTION = 1;
			Log.e("ParserCategoria", e.getMessage());
		} catch (SAXException e) {
			Log.e("ParserCategoria", e.getMessage());
		} catch (Exception e) {
			if (e.getLocalizedMessage() != null) {
				Log.e("ParserCategoria", e.getLocalizedMessage());
				Errores.NO_CONNECTION = 1;
			} else
				Log.e("ParserNegocio", "Unknown Exception");
		}
		return lista;
	}
}
